/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.just.dusin.ooii.database;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;


/**
 *
 * @author Administrator
 */
public class SyncDaoForSQLServer {
    private Connection conn = null;
    Properties props = null;
    public SyncDaoForSQLServer(Connection conn,Properties props){
        this.conn = conn;
        this.props = props;
    }
    
    public void startTransaction() throws SQLException{
        conn.setAutoCommit(false);
    }
    
    public void commitTransaction() throws SQLException{
        conn.commit();
    }
    
    public void rollbackTransaction() throws SQLException{
        conn.rollback();
    }    
    
    public List<String> getTop(int size) throws SQLException {
        List<String> list = new ArrayList<String>();
        String tableName=null;
        String primaryKey=null;
        byte operator ;
        byte status ;
        java.math.BigInteger id=null;
        String sql2 =null;
        PreparedStatement pstmt2; //修改同步表状态
        PreparedStatement pstmt3; //查询表数据        
        
        
        int fieldsCount;     //表的字段数目
        String[] fieldsName=null;
        String[] fieldsType=null;
        
        String sql1 = "select top ? tabename,primarykey,operator,status ,id from T_SYNC WHERE status=0 ORDER by ID";
        PreparedStatement pstmt = conn.prepareStatement(sql1);
        pstmt.setInt(1, size);
        ResultSet rs = pstmt.executeQuery();
        
        
        while(rs.next()){
            tableName = rs.getString(1);
            primaryKey=rs.getString(2);
            operator = rs.getByte(3);
            status = rs.getByte(4);
            id = rs.getBigDecimal(5).toBigInteger();
            
            sql2 = "update T_SYNC set status=1 where status=0 and id=?";
            pstmt2=conn.prepareStatement(sql2);
            pstmt2.setBigDecimal(1, new java.math.BigDecimal(id));
            pstmt2.executeUpdate();
            
            StringBuilder sb = new StringBuilder();
            if(operator==0){
                sb.append("delete from ").append(tableName).append(" where id=").append(primaryKey);
                list.add(sb.toString());
            }else if(operator==1){
                
                sb.append(sb);
            }                
        }        
        return list;
    } 
    
    public void changeStatus(){
        
    }
}
